const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasMore: true,
    page: 1,
    limit:10,
    total_page:0,
    itemData: [],
    lastTime:0
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    wx.showLoading({
      title: '刷新中',
    })
    this.setData({
      lastTime:0
    })
    this.loadData(this.data.page);
  },
  // onUnload: function () {
  //   wx.reLaunch({
  //     url: '/pages/index/index'
  //   })
  // },
  //页面下拉刷新
  onPullDownRefresh: function () {
    var that =  this;
  
   setTimeout(function(){
     that.resetData()
   },1000)
  },

  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },

  loadData: function (page) {
    var that = this;
    var params = {
      limit : that.data.limit,
      page : page
    }
    api.POST({
      params: params,
      url: app.globalData.orderList_url,
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var totalPage = paging.total_page;
          var currentPage = paging.page;
          var count = paging.count;
          var resultList = res.data.data.list;
          console.log(resultList);
          if(page==1){
            var itemData = resultList;
            if(itemData.length>=1){
              that.setData({
                dataHidden:"1"
              })
            }else{
              that.setData({
                dataHidden: "2"
              })
            }
          }else{
            that.setData({
              dataHidden: "1"
            })
            var  itemData=that.data.itemData.concat(resultList)
          }

          if (currentPage >= totalPage) {//数据已经加载完毕
            that.setData({
              hasMore: false,
              itemData: itemData
            });
          } else {  //存在更多的数据
            that.setData({
              hasMore: true,
              total_page: totalPage,
              itemData: itemData
            });
          }
        } else {
          util.showError(res.data.code);
        }
        wx.stopPullDownRefresh();
      },
      fail: function (fail) {
        wx.hideLoading()
        wx.stopPullDownRefresh();
        util.showError("请检查网络");
      }
    })

  },

  //加载更多
  loadMore: function () {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      page: this.data.page + 1
    });
    this.loadData(this.data.page);
  },

  //刷新页面
  resetData: function () {
    var that = this
    that.setData({
      page: 1,
      hasMore: true
    });
    that.loadData(that.data.page);
  },


  //删除订单
  delOrder:function(e){
    var that=this;
    var index = e.currentTarget.dataset.index;
    var oid = that.data.itemData[index].oid;
    wx.showModal({
      title: '',
      content: '是否删除该订单',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          api.POST({
            params: {oid:oid},
            url: app.globalData.orderDel_url,
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                // that.resetData();
                that.data.itemData.splice(index, 1);
                that.setData({
                  itemData: that.data.itemData
                })
              } else {
                util.showError(res.data.code);
              }
            },
            fail: function (fail) {
              wx.hideLoading()
              util.showError("请检查网络");
            }
          })
        } 
      }
    })
  },
  //订单信息
  orderInfo: function (oid) {
    var that = this;
    api.POST({
      params: { oid: oid },
      url: app.globalData.orderInfo_url,
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          wx.setStorageSync('orderData', res.data.data)
        }
      }
    })

  },
  //支付接口
  pay: function (e) {
    var that = this;
    var oid = e.currentTarget.dataset.oid;
    this.orderInfo(oid);
    api.POST({
      params: {oid:oid},
      url: app.globalData.base_url + 'payment/getWechatInfo',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var preData = res.data.data.payinfo;
          wx.requestPayment({
            timeStamp: preData.timeStamp,
            nonceStr: preData.nonceStr,
            package: preData.package,
            signType: preData.signType,
            paySign: preData.paySign,
            success: function (res) {
              wx.redirectTo({
                url: '../paySuccess/paySuccess?oid=' + oid
              })
            }, 
            fail: function (fail) {
              wx.removeStorageSync('orderData');
              util.showError("支付失败");
            }
          })
        }else{
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
//查看详情
  orderDetails:function(e){

    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    console.log("111")
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        return false;
      }
    }
    console.log("222")
    var oid = e.currentTarget.dataset.oid;
    wx.navigateTo({
      url: '../orderInfo/orderInfo?oid=' + oid
    })
  }
})